<?php
/**
 * 商品模型
 * // 淘宝客推广列表 => 包含淘宝推广商品转换链接的商品列表
 * 
 * @version $Id: Item.php 663 2013-01-24 07:49:52Z linsir123 $
 */
class modelItem
{
	/**
	 * @var TOP数据请求对象
	 */
	public $top;
	
	/**
	 * 构造函数
	 */
	function __construct()
	{
		$this->top = M::topRequest();
	}
	
	/**
	 * 根据参数取得【淘宝客推广列表】
	 * // 通过`taobaokeItemsGet`
	 */
	public function getTaobaokes($params, $needGruop = false)
	{
		$data = $this->top->taobaokeItemsGet($params);
		if ($needGruop) 
			$data[0] = $this->_groupCol($data[0], 3);
			
		///
		return $data;
	}
	
	/**
	 * 根据参数取得【淘宝客推广列表】
	 * // 通过`taobaokeItemsCouponGet`
	 */
	public function getTaobaokesByCoupon($params, $needGruop = false)
	{
		$data = $this->top->taobaokeItemsCouponGet($params);
		if ($needGruop) 
			$data[0] = $this->_groupCol($data[0], 3);
			
		///
		return $data;
	}
	
	/**
	 * 根据商品Id取得商品【详细信息】
	 * // 过滤描述内容的图片和链接
	 */
	public function getInfo($iid)
	{
		$data = $this->top->itemGet($iid);
		if ( ! empty($data)) {
			$propsName = array();
			$list = explode(';', $data['props_name']);
			foreach ($list as $v) 
				$propsName[] = preg_replace("/[\d]+:[\d]+:/", '', $v);
				
			///
			$data['props_name'] = $propsName;
		}
		
		///
		return $data;
	}
	
	/**
	 * 根据商铺取得【淘宝客推广列表】
	 */
	public function getPShop($sellerId)
	{
		$params = array(
			'maxCount' => 18,
			'sellerId' => $sellerId, 
			'sort' => 'commissionNum_desc', 
			'relateType' => 4, 
		);
		
		///
		$data = $this->top->taobaokeItemsRelateGet($params);
		return $this->_groupCol($data, 3);
	}
	
	/**
	 * 取得首页，商品列表
	 */
	public function getPHome()
	{
		$list = array();
		$tmp = App::O('recommendItem', 'home');
		foreach ($tmp as $i) {
			$cid = $i['cid'];
			$show = $i['show'];
			$title = $i['title'];
			$keyword = $i['keyword'];
			
			///
			$params = array('show'=>$show,'keyword'=>$keyword,'cid'=>$cid,'sort'=>'commissionVolume_desc');
			$data = $this->getTaobaokes($params);
			$items = $this->_groupCol($data[0]);
			
			///
			$keywords = M::keyword()->getList($cid, 8);
			foreach ($keywords as $v) 
				M::terms()->save($v);
				
			$list[] = array('cid'=>$cid, 'title'=>$title, 'items'=>$items, 'keywords'=>$keywords);
		}
		
		return $list;
	}
	
	
	///  以下为私有方法 ///
	
	
	/**
	 * 分组
	 */
	private function _groupCol($list, $base = 4)
	{
		$i = 0;
		$data = array();
		if (is_array($list) && ! empty($list)) {
			foreach ($list as $v) {
				$k = $i++ % $base;
				$data[$k][] = $v;
			}
		}
		
		///
		return $data;
	}
}
